﻿#include <stdio.h>
#include <stdlib.h>
#include <sys/timeb.h>
#include "GameMap.h"
#include "Snake.h"
#include "GameBase.h"
#include <windows.h>

int main(void){
	DrawGameBase();
	gotoxy(15,17);
	printf("$");
	createLinkedlist();
	return 0;
}


void gotoxy(int x, int y){
	COORD Pos = { x - 1, y - 1 };
	SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), Pos);
}

int GameState;

void InitGameBase(void){
	struct timeval LastTime;
	memset(&LastTime, 0, sizeof(struct timeval));
	GameState = GAME_STATE_PLAY;
	GetTickCount();
}


void DrawGameBase(void){
	system("cls");

	int width;
	int height;

	for (height = 0; height < Max_height; ++height){
		for (width = 0; width < Max_width; ++width){
			switch (GameMap[height][width]){
			case 0:
				printf(" ");
				break;
			case 1:
				printf("#");
				break;
			default:
				printf("?");
				break;
			}
		}
		printf("\n");
	}
}


int IsTimeToUpdate(void){
	struct timeval CurrentTime;
	struct timeval LastTime;

	memset(&LastTime, 0, sizeof(struct timeval));
	memset(&CurrentTime, 0, sizeof(struct timeval));

	GetTickCount();

	long CurrentMillisec = (CurrentTime.tv_sec * 1000) + (CurrentTime.tv_usec / 1000);
	long LastMillisec = (LastTime.tv_sec * 1000) + (LastTime.tv_usec / 1000);

	if (CurrentMillisec - LastMillisec > 500){
		LastTime = CurrentTime;
		return GAME_STATE_UPDATE;
	}
	return GAME_STATE_NOUPDATE;
}


int GameStatus(void){
	return GameState;
}

